package com.puppet.building182.aspects;

import com.alibaba.excel.enums.BooleanEnum;
import com.puppet.building182.annotation.RequirePermCheck;
import com.puppet.building182.handler.MyAssert;
import com.puppet.building182.pojo.Permission;
import com.puppet.building182.service.PermissionService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;

@Component
@Aspect
public class PermAspect {
    @Autowired
   private PermissionService permissionService ;
//    定义切入点
    @Pointcut("execution (public * com.puppet.building182.service.impl.*.*(..))")
    public void pointcut() {

    }
    @Before("pointcut()")
    public void checkPerms(JoinPoint joinPoint) {
//        查询到当前方法头顶注解中的权限编码
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
//        判断当前方法是否拥有权限检查的注解
        if (method.isAnnotationPresent(RequirePermCheck.class)) {
            RequirePermCheck annotation = method.getAnnotation(RequirePermCheck.class);
            String perm = annotation.value();
//查询当前用户的按钮权限
//            判断当前登录用户的按钮权限列表中是否存在当前方法的权限编码
            List<String> userButtonPerms = permissionService.getAllButtonPerms();
            boolean flg=userButtonPerms.contains(perm);
//            当前方法不具备这个方法的按钮权限
//            如果不存在，直接断言抛出异常
            MyAssert.error(!flg,"您没有该权限");
        }
//        如果存在，则正常执行
    }



}
//切面
